package com.jlect.swebing.renderers.gwt.client;

import com.google.gwt.user.client.Element;
import com.jlect.swebing.renderers.client.Renderer;

import java.util.HashMap;
import java.util.Map;

/**
 * Store and register information about what DOM component (i.e. widget) is bound to what renderer
 *
 * @author Sergey Kozmin
 * @since 25.11.2007 15:47:32
 */
public class RenderersRegister {
    private static RenderersRegister INSTANCE = new RenderersRegister();

    private Map components = new HashMap();

    private RenderersRegister() {
    }

    /**
     * Retrievs component that is bound to the given renderer
     * @param element dom element
     * @return bound component
     */
    public Renderer getRenderer(Element element) {
        return (Renderer) components.get(element);
    }

    /**
     * Retisters the given renderer and its DOM element.
     * @param element element that belongs to the renderer
     * @param renderer renderer to register
     */
    public void registerRenderer(Element element, Renderer renderer) {
        components.put(element, renderer);
    }

    /**
     * Cancels the registration of the component
     * @param element DOM element to be unregistered
     * @return registered renderer
     */
    public Renderer cancelRegistration(Element element) {
        return (Renderer) components.remove(element);
    }

    public static RenderersRegister getRegister() {
        return INSTANCE;
    }
}